package cn.zzp.pet.service.impl;

import cn.zzp.basic.service.impl.BaseServiceImpl;
import cn.zzp.pet.domain.Pet;
import cn.zzp.pet.domain.PetType;
import cn.zzp.pet.mapper.PetTypeMapper;
import cn.zzp.pet.service.PetService;
import cn.zzp.pet.service.PetTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * @ClassName EmployeeServiceImpl
 * @Description
 * @Author 祝志鹏
 * @Date 2022/7/14 14:16
 */
@Service
//@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class PetTypeServiceImpl extends BaseServiceImpl<PetType> implements PetTypeService {
    @Autowired
    private PetTypeMapper petTypeMapper;

    @Override
    public List<PetType> typeTree() {
        List<PetType> petTypes = petTypeMapper.queryAll();
        HashMap<Long, PetType> map = new HashMap<>();

        for (PetType petType : petTypes) {
            map.put(petType.getId(), petType);
        }

        ArrayList<PetType> typeTree = new ArrayList<>();

        for (PetType petType : petTypes) {

            if (petType.getPid() == null){
                typeTree.add(petType);
            } else {
                Long pid = petType.getPid();
                map.get(pid).getChildren().add(petType);
            }

        }
        return typeTree;
    }
}
